package leecode.easy;

import org.junit.Test;

/**
 * @Author: xieZW
 * @Date: 2022/2/10 10:30
 * 给定一个数组 prices ，它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
 * <p>
 * 你只能选择 某一天 买入这只股票，并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
 * <p>
 * 返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润，返回 0 。
 * 输入：[7,1,5,3,6,4]
 * 输出：5
 */
public class Index121 {

    @Test
    public void fun() {
        int[] a = {897, 265, 201, 86, 56, 657, 273, 25, 843, 625, 641, 332, 509, 463, 496, 97, 779, 241, 970, 665, 87, 765, 276, 442, 25, 311, 10, 765, 224, 465, 296, 415, 968, 40, 494, 151, 493, 159, 274, 444, 10, 792, 104, 200, 694, 193, 616, 126, 754, 3, 489, 415, 863, 589, 646, 264, 88, 733, 132, 922, 794, 149, 496, 135, 727, 355, 890, 102, 3, 668, 517, 852, 488, 445, 980, 964, 933, 754, 815, 496, 550, 2, 33, 191, 516, 455, 436, 41, 618, 596, 334, 10, 708, 348, 511, 752, 767, 129, 470, 698, 647, 604, 104, 811, 574, 52, 750, 319, 97, 45, 391, 42, 822, 712, 92, 664, 318, 702, 5, 303, 778, 799, 638, 430, 66, 944, 78, 40, 632, 919, 828, 851, 589, 75, 328, 774, 991, 305, 126, 996, 505, 463, 428, 972, 781, 697, 244, 98, 148, 670, 30, 328, 457, 673, 766, 69, 461, 44, 804, 986, 133, 169, 786, 950, 745, 63, 362, 857, 411, 12, 680, 9, 586, 827, 932, 54, 538, 509, 120, 287, 534, 866, 926, 351, 634, 842, 188, 57, 443, 296, 134, 254, 447, 350, 558, 418, 917, 492, 683, 702, 504, 964, 319, 717, 178, 400, 226, 66, 969, 183, 703, 231, 38, 684, 197, 245, 600, 97, 126, 536, 612, 54, 414, 827, 598, 4, 368, 412, 248, 293, 270, 937, 847, 265, 801, 278, 64, 600, 254, 54, 791, 608, 782, 532, 719, 76, 224, 981, 695, 727, 987, 567, 230, 149, 807, 721, 0, 301, 881, 713, 514, 196, 67};
//        int[] a = {7, 1, 5, 3, 6, 4};
//        int[] a = {7, 6, 4, 3, 1};
        int i = maxProfit(a);
        System.out.println("i = " + i);
    }

    public int maxProfit(int[] prices) {

        int small = Integer.MAX_VALUE;
        int max = 0;

        for (int i = 0; i < prices.length; i++) { //买
            if (small > prices[i]) {
                small = prices[i];
            } else if (prices[i] - small > max) {
                max = prices[i] - small;
            }
        }
        return max > 0 ? max : 0;
    }
}
